<?

//Data loading
$title='STATEMENT OF CASH FLOWS (OPERATIONAL)';
if (GetParam("office","")) $perush = GetCompany (GetParam("office",""));
else $perush = 'PT. MASAJI PRAYASA CARGO';

$page   = 'PAGE';

$DEFINE_FONT=0;
$pdf=new GridPDFReport("P","mm","Letter");
$fromDate = GetParam("fromDate","");
$toDate  = GetParam("toDate","");
$pdf->AddPage();
//$Result = GridList ();
$pdf->GridTable();
$pdf->Output();

function Neraca ($PYear,$PMonth) {
    global $DBConnection;

    //$TEMP = "TEMPORARY";

    $Year   = $PYear;
    $LastYear = $Year-1;
    $Month  = $PMonth;
    if ($Month=="AUDIT") $Month=13;
    if ($Month<10) $Month = "0".$Month;

    if (($Month-1)<10) $LastMonth = "0".($Month-1);
    else $LastMonth = $Month-1;

    if ($Month<2) $LastMonth = "awal";
    else {$LastMonth = "aku".$LastMonth;}

    $Mutasi = "(mutd".$Month."-"."mutk".$Month.") as mutasi";
    $CurrentMonth = "aku".$Month." as cmonth";

    $SQL = "DROP TABLE IF EXISTS `tblcoatmp`";
    $DBConnection->dbc->query($SQL);

    $SQL = "DROP TABLE IF EXISTS `tblcoatmp_last`";
    $DBConnection->dbc->query($SQL);

    $SQL = "
        CREATE $TEMP TABLE `tblcoatmp` (
        `skada` VARCHAR( 64 ) NOT NULL,
        `pathprn` VARCHAR( 64 ) NOT NULL,
        `namacc` VARCHAR( 64 ) NOT NULL,
        `parent` VARCHAR( 24 ) NOT NULL,
        `printing` tinyint( 4 ) NOT NULL,
        `jumlah` tinyint( 4 ) NOT NULL,
        `grup` tinyint( 4 ) NOT NULL,
        `akumulasi` decimal( 16,4) NOT NULL,
        `lastakumulasi` decimal( 16,4) NOT NULL,
        `lastmonth` decimal( 16,4) NOT NULL,
        `ktrasl` VARCHAR( 4 ) NOT NULL
        );
    ";
    $DBConnection->dbc->query($SQL);

    $SQL = "
        CREATE $TEMP TABLE `tblcoatmp_last` (
        `skada` VARCHAR( 64 ) NOT NULL,
        `akumulasi` decimal( 16,4) NOT NULL,
                `ktrasl` VARCHAR( 4 ) NOT NULL
        );
    ";
    $DBConnection->dbc->query($SQL);

    $SQL = "
        INSERT INTO tblcoatmp_last
        SELECT skada, $CurrentMonth, ktrasl
        FROM tblcoa WHERE tipe=1 AND ledger='$LastYear'
    ";

    if (GetParam("office",""))  $SQL .= "AND tblcoa.ktrasl='".GetParam("office","")."'";
    $DBConnection->dbc->query($SQL);

    $SQL = "
        INSERT INTO tblcoatmp
        SELECT tblcoa.skada, tblcoa.pathprn, tblcoa.namacc, tblcoa.parent, tblcoa.printing, tblcoa.jumlah, tblcoa.grup, $CurrentMonth, tblcoatmp_last.akumulasi, $LastMonth, tblcoa.ktrasl
        FROM tblcoa LEFT JOIN tblcoatmp_last ON tblcoatmp_last.skada=tblcoa.skada AND tblcoatmp_last.ktrasl=tblcoa.ktrasl
        WHERE tipe=1 AND ledger='$Year'
    ";

    if (GetParam("office",""))      $SQL .= "AND tblcoa.ktrasl='".GetParam("office","")."'";
    $DBConnection->dbc->query($SQL);

    $SQL = "DROP TABLE IF EXISTS `tblcoa_totaltmp`";
    $DBConnection->dbc->query($SQL);

    $SQL = "
        CREATE $TEMP TABLE `tblcoa_totaltmp` (
        `skada` VARCHAR( 64 ) NOT NULL,
        `pathprn` VARCHAR( 64 ) NOT NULL,
        `namacc` VARCHAR( 64 ) NOT NULL,
        `parent` VARCHAR( 24 ) NOT NULL,
        `printing` tinyint( 4 ) NOT NULL,
        `jumlah` tinyint( 4 ) NOT NULL,
        `grup` tinyint( 4 ) NOT NULL,
        `mutd` decimal( 16,4) NOT NULL,
        `mutk` decimal( 16,4) NOT NULL,
        `akumulasi` decimal( 16,4) NOT NULL,
        `lastakumulasi` decimal( 16,4) NOT NULL,
        `lastmonth` decimal( 16,4) NOT NULL,
        `ktrasl` VARCHAR( 4 ) NOT NULL
        );
    ";
    $DBConnection->dbc->query($SQL);

    $SQL  = "
        INSERT INTO tblcoa_totaltmp
        SELECT tblcoatmp.skada, tblcoatmp.pathprn, tblcoatmp.namacc, tblcoatmp.parent, tblcoatmp.printing,
                tblcoatmp.jumlah, tblcoatmp.grup, sum(mutd$Month), sum(mutk$Month), sum(aku$Month), lastakumulasi, sum($LastMonth), tblcoa.ktrasl
        FROM tblcoatmp, tblcoa
        WHERE tblcoa.pathprn like concat(tblcoatmp.pathprn,'%') AND tblcoatmp.ktrasl=tblcoa.ktrasl
    ";

    if (GetParam("office","")) $SQL .= " AND tblcoa.ktrasl='".GetParam("office","")."' ";

    $SQL .= "GROUP BY tblcoatmp.skada";
    //print $SQL."<p>\n";die();
    $DBConnection->dbc->query($SQL);
}

function FlushCustomer ($TableName, $tmp) {
    global $DBConnection;

    if ($tmp) $TEMPORARY = "TEMPORARY";
    $DBC = new mydb(DB_USER, DB_PASSWORD, DB_NAME2, DB_HOST);

    $SQL = "SELECT kodecustomer,nama,cr_term,kdtype FROM customer ";
    if (GetParam("kdtype","")) $SQL .= " WHERE kdtype='".GetParam("kdtype","")."'";
    $MyResult = $DBC->dbc->get_results($SQL,ARRAY_N);
    if (!$MyResult) return false;
    $session_file = substr(md5(time()), 0, 8);
    $session_file = "/tmp/".$session_file.".tmp";
    BufferToFile ($MyResult,$session_file);

    $DBConnection = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);
        $TableNamea = "ais_cust";

    $SQL = "DROP TABLE IF EXISTS `$TableNamea`;";
    $DBConnection->dbc->query($SQL);

    $SQL= "CREATE $TEMPORARY TABLE `$TableNamea` (
              `kodecustomer` varchar(20) NOT NULL default '',
              `nama` varchar(50) default NULL,
              `cr_term` int(4) default NULL,
              `kdtype` varchar(20) default NULL
              )";
    $DBConnection->dbc->query($SQL);

    $SQL = "LOAD DATA INFILE '$session_file' INTO TABLE $TableNamea FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\n'";
    $DBConnection->dbc->query($SQL);
        $SQL = "INSERT INTO $TableNamea SELECT kdvendor,nama,top,kdtype FROM tblvendor ";
        $DBConnection->dbc->query($SQL);
/*
        $SQL = "INSERT INTO $TableNamea SELECT kdother,nama,'30','' FROM tblother ";
        $DBConnection->dbc->query($SQL);
  */
    $SQL = "DROP TABLE IF EXISTS `$TableName`;";
    $DBConnection->dbc->query($SQL);

    $SQL= "CREATE $TEMPORARY TABLE `$TableName` (
              `kodecustomer` varchar(20) NOT NULL default '',
              `nama` varchar(50) default NULL,
              `cr_term` int(4) default NULL,
              `kdtype` varchar(20) default NULL
              )";
    $DBConnection->dbc->query($SQL);
        $SQL = "INSERT INTO `$TableName` SELECT * FROM $TableNamea GROUP BY kodecustomer ";
        $DBConnection->dbc->query($SQL);

    unlink($session_file);
    return true;
}

function GetKas($Year,$Month) {
    global $DBConnection;
    if (($Month-1)<10) $LastMonth = "0".($Month-1);
    else $LastMonth = $Month-1;

    if ($Month<2) $LastMonth = "awal";
    else {$LastMonth = "aku".$LastMonth;}

    $SQL = "SELECT SUM($LastMonth) FROM tblcoa WHERE skada like '000101%' AND ledger=$Year";
    $Value[0] = $DBConnection->dbc->get_var($SQL);

    $SQL = "SELECT SUM($LastMonth) FROM tblcoa WHERE skada like '000102%' AND ledger=$Year";
    $Value[1] = $DBConnection->dbc->get_var($SQL);
    $Value[1] = $Value[1]/StrToNumber(GetParam("lastKurs",""));
    return $Value;
}

function GetPiutangOthers($fromDate,$toDate) {
    global $DBConnection;

    $SQL = "
            SELECT SUM(rupiah) FROM ais_memorial, tblstaff
            WHERE
            nobkt like 'T%' AND tblstaff.nik=ais_memorial.debtur
            AND skada NOT like '5001%' AND NOT (skada like '000101%' OR skada like '00020%') AND tglbkt>='$fromDate' AND tglbkt<='$toDate'
            AND curr='IDR'
            ";
    $Value_staff = $DBConnection->dbc->get_var($SQL);

    $SQL = "
            SELECT SUM(rupiah) FROM ais_memorial, tblother
            WHERE
            nobkt like 'T%' AND tblother.kdother=ais_memorial.debtur
            AND skada NOT like '5001%' AND NOT (skada like '000101%' OR skada like '00020%') AND tglbkt>='$fromDate' AND tglbkt<='$toDate'
            AND curr='IDR'
            ";
    $Value_other = $DBConnection->dbc->get_var($SQL);

    $SQL = "
            SELECT SUM(rupiah) FROM ais_memorial
            WHERE
            nobkt like 'T%'
            AND NOT (skada like '000101%' OR skada like '00020%')
            AND skada NOT like '5001%' AND tglbkt>='$fromDate' AND tglbkt<='$toDate' AND ifnull(debtur,'')=''
            AND curr='IDR'
            ";
    $Value_blank = $DBConnection->dbc->get_var($SQL);

    $Value[0] = $Value_staff+$Value_other+$Value_blank;

    $SQL = "
            SELECT SUM(jumlah) FROM ais_memorial, tblstaff
            WHERE
            nobkt like 'T%' AND tblstaff.nik=ais_memorial.debtur
            AND skada NOT like '5001%' AND NOT (skada like '000102%' OR skada like '00021%') AND tglbkt>='$fromDate' AND tglbkt<='$toDate'
            AND curr='USD'
            ";
    $Value_staff = $DBConnection->dbc->get_var($SQL);

    $SQL = "
            SELECT SUM(jumlah) FROM ais_memorial, tblother
            WHERE
            nobkt like 'T%' AND tblother.kdother=ais_memorial.debtur
            AND skada NOT like '5001%' AND NOT (skada like '000102%' OR skada like '00021%') AND tglbkt>='$fromDate' AND tglbkt<='$toDate'
            AND curr='USD'
            ";
    $Value_other = $DBConnection->dbc->get_var($SQL);

    $SQL = "
            SELECT SUM(rupiah) FROM ais_memorial
            WHERE
            nobkt like 'T%'
            AND NOT (skada like '000102%' OR skada like '00021%')
            AND skada NOT like '5001%' AND tglbkt>='$fromDate' AND tglbkt<='$toDate' AND debtur=''
            AND curr='USD'
            ";
    $Value_blank = $DBConnection->dbc->get_var($SQL);

    $Value[1] = $Value_staff+$Value_other;
    $Value[1] = $Value[1];
    return $Value;
}

function GetPiutangAfiliasi($fromDate,$toDate) {
    global $DBConnection;

    $SQL = "SELECT SUM(rupiah) FROM ais_memorial, ais_customer WHERE NOT (skada like '000101%' OR skada like '00020%') AND nobkt like 'T%' AND ais_customer.kodecustomer=ais_memorial.debtur AND (ais_customer.kdtype='CC002' OR ais_customer.kdtype='CC003' OR ais_customer.kdtype='VC002' OR ais_customer.kdtype='VC003') AND skada NOT like '5001%' AND tglbkt>='$fromDate' AND tglbkt<='$toDate' AND curr='IDR'";
    $Value[0] = $DBConnection->dbc->get_var($SQL);

    $SQL = "SELECT SUM(jumlah) FROM ais_memorial, ais_customer WHERE NOT (skada like '000102%' OR skada like '00021%') AND nobkt like 'T%' AND ais_customer.kodecustomer=ais_memorial.debtur AND (ais_customer.kdtype='CC002' OR ais_customer.kdtype='CC003' OR ais_customer.kdtype='VC002' OR ais_customer.kdtype='VC003') AND skada NOT like '5001%' AND tglbkt>='$fromDate' AND tglbkt<='$toDate' AND curr='USD'";
    $Value[1] = $DBConnection->dbc->get_var($SQL);
    $Value[1] = $Value[1];
    return $Value;
}

function GetPiutangNonAfiliasi($fromDate,$toDate) {
    global $DBConnection;

    $SQL = "SELECT SUM(rupiah) FROM ais_memorial, ais_customer WHERE NOT (skada like '000101%' OR skada like '00020%') AND nobkt like 'T%' AND ais_customer.kodecustomer=ais_memorial.debtur AND (ais_customer.kdtype='CC001' OR ais_customer.kdtype='VC001') AND skada NOT like '5001%' AND tglbkt>='$fromDate' AND tglbkt<='$toDate' AND curr='IDR'";
    $Value[0] = $DBConnection->dbc->get_var($SQL);

    $SQL = "SELECT SUM(jumlah) FROM ais_memorial, ais_customer WHERE NOT (skada like '000102%' OR skada like '00021%') AND nobkt like 'T%' AND ais_customer.kodecustomer=ais_memorial.debtur AND (ais_customer.kdtype='CC001' OR ais_customer.kdtype='VC001') AND skada NOT like '5001%' AND tglbkt>='$fromDate' AND tglbkt<='$toDate' AND curr='USD'";
    $Value[1] = $DBConnection->dbc->get_var($SQL);
    $Value[1] = $Value[1];
    return $Value;
}


function GetBank($Year,$Month) {
    global $DBConnection;

    if (($Month-1)<10) $LastMonth = "0".($Month-1);
    else $LastMonth = $Month-1;

    if ($Month<2) $LastMonth = "awal";
    else {$LastMonth = "aku".$LastMonth;}

    $SQL = "SELECT SUM($LastMonth) FROM tblcoa WHERE skada like '00020%' AND ledger=$Year";
    $Value[0] = $DBConnection->dbc->get_var($SQL);

    $SQL = "SELECT SUM($LastMonth) FROM tblcoa WHERE skada like '00021%' AND ledger=$Year";
    $Value[1] = $DBConnection->dbc->get_var($SQL);
    $Value[1] = $Value[1]/StrToNumber(GetParam("lastKurs",""));

    return $Value;
}

function GetDeposito($Year,$Month) {
    global $DBConnection;
    if (($Month-1)<10) $LastMonth = "0".($Month-1);
    else $LastMonth = $Month-1;

    if ($Month<2) $LastMonth = "awal";
    else {$LastMonth = "aku".$LastMonth;}

    $SQL = "SELECT SUM($LastMonth) FROM tblcoa WHERE skada like '00030%' AND ledger=$Year";
    $Value[0] = $DBConnection->dbc->get_var($SQL);

    $SQL = "SELECT SUM($LastMonth) FROM tblcoa WHERE skada like '00031%' AND ledger=$Year";
    $Value[1] = $DBConnection->dbc->get_var($SQL);
    $Value[1] = $Value[1]/StrToNumber(GetParam("lastKurs",""));
    return $Value;

}

function GetPaidInCapital($Year,$Month) {
    global $DBConnection;

    $SQL = "SELECT SUM(rupiah) FROM ais_memorial WHERE nobkt like 'T%' AND skada like '50%'";
    $Value[0] = $DBConnection->dbc->get_var($SQL);

    $SQL = "SELECT '0' FROM ais_memorial WHERE nobkt like 'T%' AND skada like '50%'";
    $Value[1] = $DBConnection->dbc->get_var($SQL);
    $Value[1] = $Value[1];
    return $Value;

}

function GetOverhead($fromDate,$toDate) {
    global $DBConnection;

    $SQL = "
            SELECT SUM(rupiah) FROM ais_memorial, tblstaff
            WHERE
            nobkt like 'B%' AND tblstaff.nik=ais_memorial.debtur
            AND NOT (skada like '000101%' OR skada like '00020%')
            AND skada like '8%' AND tglbkt>='$fromDate' AND tglbkt<='$toDate'
            AND curr='IDR'
            ";
    $Value_staff = $DBConnection->dbc->get_var($SQL);

    $SQL = "
            SELECT SUM(rupiah) FROM ais_memorial, tblother
            WHERE
            nobkt like 'B%' AND tblother.kdother=ais_memorial.debtur
            AND NOT (skada like '000101%' OR skada like '00020%')
            AND skada like '8%' AND tglbkt>='$fromDate' AND tglbkt<='$toDate'
            AND curr='IDR'
            ";
    $Value_other = $DBConnection->dbc->get_var($SQL);

    $SQL = "
            SELECT SUM(rupiah) FROM ais_memorial
            WHERE
            nobkt like 'B%'
            AND NOT (skada like '000101%' OR skada like '00020%')
            AND skada like '8%' AND tglbkt>='$fromDate' AND tglbkt<='$toDate' AND debtur=''
            AND curr='IDR'
            ";
    $Value_blank = $DBConnection->dbc->get_var($SQL);


    $Value[0] = $Value_staff+$Value_other+$Value_blank;

    $SQL = "
            SELECT SUM(jumlah) FROM ais_memorial, tblstaff
            WHERE
            nobkt like 'B%' AND tblstaff.nik=ais_memorial.debtur
            AND NOT (skada like '000102%' OR skada like '00021%')
            AND skada like '8%' AND tglbkt>='$fromDate' AND tglbkt<='$toDate'
            AND curr='USD'
            ";
    $Value_staff = $DBConnection->dbc->get_var($SQL);

    $SQL = "
            SELECT SUM(jumlah) FROM ais_memorial, tblother
            WHERE
            nobkt like 'B%' AND tblother.kdother=ais_memorial.debtur
            AND NOT (skada like '000102%' OR skada like '00021%')
            AND skada like '8%' AND tglbkt>='$fromDate' AND tglbkt<='$toDate'
            AND curr='USD'
            ";
    $Value_other = $DBConnection->dbc->get_var($SQL);

    $SQL = "
            SELECT SUM(jumlah) FROM ais_memorial
            WHERE
            nobkt like 'B%'
            AND NOT (skada like '000102%' OR skada like '00021%')
            AND skada like '8%' AND tglbkt>='$fromDate' AND tglbkt<='$toDate' AND debtur=''
            AND curr='USD'
            ";
    $Value_blank = $DBConnection->dbc->get_var($SQL);


    $Value[1] = $Value_staff+$Value_other+$Value_blank;
    $Value[1] = $Value[1];
    return $Value;
}

function GetHutangAfiliasi($fromDate,$toDate) {
    global $DBConnection;

    $SQL = "SELECT SUM(rupiah) FROM ais_memorial, ais_customer WHERE NOT (skada like '000101%' OR skada like '00020%') AND nobkt like 'B%' AND ais_customer.kodecustomer=ais_memorial.debtur AND (ais_customer.kdtype='CC002' OR ais_customer.kdtype='CC003' OR ais_customer.kdtype='VC002' OR ais_customer.kdtype='VC003') AND skada NOT like '9002%' AND skada NOT like '3006%' AND skada NOT like '3007%' AND skada NOT like '8%' AND tglbkt>='$fromDate' AND tglbkt<='$toDate' AND curr='IDR'";
    $Value[0] = $DBConnection->dbc->get_var($SQL);

    $SQL = "SELECT SUM(jumlah) FROM ais_memorial, ais_customer WHERE NOT (skada like '000102%' OR skada like '00021%') AND nobkt like 'B%' AND ais_customer.kodecustomer=ais_memorial.debtur AND (ais_customer.kdtype='CC002' OR ais_customer.kdtype='CC003' OR ais_customer.kdtype='VC002' OR ais_customer.kdtype='VC003') AND skada NOT like '9002%' AND skada NOT like '3006%' AND skada NOT like '3007%' AND skada NOT like '8%' AND tglbkt>='$fromDate' AND tglbkt<='$toDate' AND curr='USD'";
    $Value[1] = $DBConnection->dbc->get_var($SQL);
    $Value[1] = $Value[1];
    return $Value;
}

function GetHutangNonAfiliasi($fromDate,$toDate) {
    global $DBConnection;

    $SQL = "SELECT SUM(rupiah) FROM ais_memorial, ais_customer WHERE NOT (skada like '000101%' OR skada like '00020%') AND nobkt like 'B%' AND ais_customer.kodecustomer=ais_memorial.debtur AND (ais_customer.kdtype='CC001' OR ais_customer.kdtype='VC001') AND skada NOT like '9002%' AND skada NOT like '3006%' AND skada NOT like '3007%' AND skada NOT like '8%' AND tglbkt>='$fromDate' AND tglbkt<='$toDate' AND curr='IDR'";
    $Value[0] = $DBConnection->dbc->get_var($SQL);

    $SQL = "SELECT SUM(jumlah) FROM ais_memorial, ais_customer WHERE NOT (skada like '000102%' OR skada like '00021%') AND nobkt like 'B%' AND ais_customer.kodecustomer=ais_memorial.debtur AND (ais_customer.kdtype='CC001' OR ais_customer.kdtype='VC001') AND skada NOT like '9002%' AND skada NOT like '3006%' AND skada NOT like '3007%' AND skada NOT like '8%' AND tglbkt>='$fromDate' AND tglbkt<='$toDate' AND curr='USD'";
    $Value[1] = $DBConnection->dbc->get_var($SQL);
    $Value[1] = $Value[1];
    return $Value;
}

function GetLoanPrincipal($fromDate,$toDate) {
    global $DBConnection;

    $SQL = "SELECT SUM(rupiah) FROM ais_memorial WHERE (skada like '30060%' OR skada like '30070%') AND nobkt like 'B%' AND tglbkt>='$fromDate' AND tglbkt<='$toDate'";
    $Value[0] = $DBConnection->dbc->get_var($SQL);

    $SQL = "SELECT SUM(jumlah) FROM ais_memorial WHERE (skada like '30061%' OR skada like '30071%') AND nobkt like 'B%' AND tglbkt>='$fromDate' AND tglbkt<='$toDate'";
    $Value[1] = $DBConnection->dbc->get_var($SQL);
    $Value[1] = $Value[1];
    return $Value;
}

function GetInterestPayment($fromDate,$toDate) {
    global $DBConnection;

    $SQL = "SELECT SUM(rupiah) FROM ais_memorial WHERE skada like '90020%' AND nobkt like 'B%' AND tglbkt>='$fromDate' AND tglbkt<='$toDate'";
    //print $SQL;
    $Value[0] = $DBConnection->dbc->get_var($SQL);

    $SQL = "SELECT SUM(jumlah) FROM ais_memorial WHERE skada like '90021%' AND nobkt like 'B%' AND tglbkt>='$fromDate' AND tglbkt<='$toDate'";
    $Value[1] = $DBConnection->dbc->get_var($SQL);
    $Value[1] = $Value[1];
    return $Value;
}


function GetCashFlow() {
    list($TDay,$TMonth,$TYear) = explode("/",GetParam("toDate",""));
    $FDay=1;$FMonth=$TMonth;$FYear=$TYear;
    $fromDate = "$FYear-$FMonth-$FDay";
    $toDate   = "$TYear-$TMonth-$TDay";
    $TMonth = $TMonth*1;
    Neraca ($TYear,$TMonth);
    FlushCustomer ("ais_customer", 0);
    list($result["KAS_IDR"],$result["KAS_USD"]) = GetKas($TYear,$TMonth);
    list($result["BANK_IDR"],$result["BANK_USD"]) = GetBank($TYear,$TMonth);
    list($result["DEPOSITO_IDR"],$result["DEPOSITO_USD"]) = GetDeposito($TYear,$TMonth);
    $result["TOTAL_AWAL_IDR"] = $result["KAS_IDR"]+$result["BANK_IDR"]+$result["DEPOSITO_IDR"];
    $result["TOTAL_AWAL_USD"] = $result["KAS_USD"]+$result["BANK_USD"]+$result["DEPOSITO_USD"];
    list($result["PIUTANGAFILIASI_IDR"],$result["PIUTANGAFILIASI_USD"]) = GetPiutangAfiliasi($fromDate,$toDate);
    list($result["PIUTANGNONAFILIASI_IDR"],$result["PIUTANGNONAFILIASI_USD"]) = GetPiutangNonAfiliasi($fromDate,$toDate);
    list($result["PIUTANGOTHER_IDR"],$result["PIUTANGOTHER_USD"]) = GetPiutangOthers($fromDate,$toDate);
    $result["SUBTOTAL_OPSCASHIN_IDR"] = $result["PIUTANGAFILIASI_IDR"]+$result["PIUTANGNONAFILIASI_IDR"]+$result["PIUTANGOTHER_IDR"];
    $result["SUBTOTAL_OPSCASHIN_USD"] = $result["PIUTANGAFILIASI_USD"]+$result["PIUTANGNONAFILIASI_USD"]+$result["PIUTANGOTHER_USD"];
    list($result["PAID_IN_CAPITAL_IDR"],$result["PAID_IN_CAPITAL_USD"]) = GetPaidInCapital($Year,$Month);
    $result["TOTAL_CASHIN_IDR"] = $result["PAID_IN_CAPITAL_IDR"]+$result["SUBTOTAL_OPSCASHIN_IDR"];
    $result["TOTAL_CASHIN_USD"] = $result["PAID_IN_CAPITAL_USD"]+$result["SUBTOTAL_OPSCASHIN_USD"];
    $result["CASH_AVAILABLE_IDR"] = $result["TOTAL_CASHIN_IDR"]+$result["TOTAL_AWAL_IDR"];
    $result["CASH_AVAILABLE_USD"] = $result["TOTAL_CASHIN_USD"]+$result["TOTAL_AWAL_USD"];
    list($result["HUTANGAFILIASI_IDR"],$result["HUTANGAFILIASI_USD"]) = GetHutangAfiliasi($fromDate,$toDate);
    list($result["HUTANGNONAFILIASI_IDR"],$result["HUTANGNONAFILIASI_USD"]) = GetHutangNonAfiliasi($fromDate,$toDate);
    list($result["OVERHEAD_IDR"],$result["OVERHEAD_USD"]) = GetOverhead($fromDate,$toDate);
    $result["SUBTOTAL_OPSCASHOUT_IDR"] = $result["HUTANGAFILIASI_IDR"]+$result["HUTANGNONAFILIASI_IDR"]+$result["OVERHEAD_IDR"];
    $result["SUBTOTAL_OPSCASHOUT_USD"] = $result["HUTANGAFILIASI_USD"]+$result["HUTANGNONAFILIASI_USD"]+$result["OVERHEAD_USD"];
    list($result["LOAN_PRINCIPAL_IDR"],$result["LOAN_PRINCIPAL_USD"]) = GetLoanPrincipal($fromDate,$toDate);
    list($result["INTEREST_PAYMENT_IDR"],$result["INTEREST_PAYMENT_USD"]) = GetInterestPayment($fromDate,$toDate);
    $result["CASH_OUT_IDR"] = $result["LOAN_PRINCIPAL_IDR"]+$result["INTEREST_PAYMENT_IDR"];
    $result["CASH_OUT_USD"] = $result["LOAN_PRINCIPAL_USD"]+$result["INTEREST_PAYMENT_USD"];
    $result["TOTAL_CASH_OUT_IDR"] = $result["CASH_OUT_IDR"]+$result["SUBTOTAL_OPSCASHOUT_IDR"];
    $result["TOTAL_CASH_OUT_USD"] = $result["CASH_OUT_USD"]+$result["SUBTOTAL_OPSCASHOUT_USD"];
    $result["ENDING_BALANCE_IDR"] = $result["CASH_AVAILABLE_IDR"]-$result["TOTAL_CASH_OUT_IDR"];
    $result["ENDING_BALANCE_USD"] = $result["CASH_AVAILABLE_USD"]-$result["TOTAL_CASH_OUT_USD"];
    return $result;
}

class GridPDFReport extends PDF {
    function Header() {
        global $title,$perush,$page,$DEFINE_FONT;
        if ($DEFINE_FONT==0) {
            $this->AddFont('arialn','','arialn.php');
            $DEFINE_FONT=1;
        }
        //$this->SetLeftMargin(5);
        list($day,$month,$year) = explode("/",GetParam("toDate",""));

        $this->SetFont('arialn','',10);
        $this->Cell(200,5,$perush,0,1,'C',0);
        $this->Cell(200,5,$title,0,1,'C',0);
        $this->Cell(200,5,"PERIOD: "."01/$month/$year"." TO: "."$day/$month/$year",0,1,'C',0);
        $this->SetLeftMargin(5);
        $this->Cell(200,5,$page." ".$this->PageNo(),0,1,'R',0);
        $this->Line(5,30,210,30);
        $this->Ln();
        $this->Ln();
        //Header
        //Column widths
        $w=array(75,30,30,30);
        //$this->SetFont('arialn','',10);

        $this->Cell($w[0],7,"DESCRIPTION",1,0,'C',0);
        $this->Cell($w[1],7,"IDR",1,0,'C',0);
        $this->Cell($w[2],7,"USD",1,0,'C',0);
        $this->Ln();

        $this->SetFont('arialn','',8);
    }



    function GridTable() {
        //Column widths


        $Current = GetCashFlow();
        $w=array(75,30,30,30);
        $this->Cell($w[0],5,"BEGINNING BALANCE:","LR",0,'L',0);
        $this->Cell($w[1],5,"","LR",0,'L',0);
        $this->Cell($w[2],5,"","LR",0,'L',0);
        $this->Ln();
        $this->Cell($w[0],5,"  - CASH","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Current["KAS_IDR"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Current["KAS_USD"],2),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"  - BANK","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Current["BANK_IDR"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Current["BANK_USD"],2),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"  - DEPOSITS","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Current["DEPOSITO_IDR"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Current["DEPOSITO_USD"],2),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],7,"TOTAL BEGINNING BALANCE",1,0,'R',0);
        $this->Cell($w[1],7,FormatNumber($Current["TOTAL_AWAL_IDR"]),1,0,'R',0);
        $this->Cell($w[2],7,FormatNumber($Current["TOTAL_AWAL_USD"],2),1,0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"CASH IN FLOW","LR",0,'L',0);
        $this->Cell($w[1],5,"","LR",0,'R',0);
        $this->Cell($w[2],5,"","LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"OPERATIONAL CASH IN FLOW","LR",0,'L',0);
        $this->Cell($w[1],5,"","LR",0,'R',0);
        $this->Cell($w[2],5,"","LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"  - RECEIVED FROM AFFILIATED","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Current["PIUTANGAFILIASI_IDR"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Current["PIUTANGAFILIASI_USD"],2),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"  - RECEIVED FROM THIRD PARTY","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Current["PIUTANGNONAFILIASI_IDR"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Current["PIUTANGNONAFILIASI_USD"],2),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"  - OTHERS","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Current["PIUTANGOTHER_IDR"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Current["PIUTANGOTHER_USD"],2),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],7,"SUB TOTAL OPR CASH IN","LR",0,'R',0);
        $this->Cell($w[1],7,FormatNumber($Current["SUBTOTAL_OPSCASHIN_IDR"]),"LR",0,'R',0);
        $this->Cell($w[2],7,FormatNumber($Current["SUBTOTAL_OPSCASHIN_USD"],2),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"NON OPERATIONAL CASH IN FLOW","LR",0,'L',0);
        $this->Cell($w[1],5,"","LR",0,'L',0);
        $this->Cell($w[2],5,"","LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"   - PAID IN CAPITAL","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Current["PAID_IN_CAPITAL_IDR"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Current["PAID_IN_CAPITAL_USD"],2),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],7,"TOTAL CASH IN",1,0,'R',0);
        $this->Cell($w[1],7,FormatNumber($Current["TOTAL_CASHIN_IDR"]),1,0,'R',0);
        $this->Cell($w[2],7,FormatNumber($Current["TOTAL_CASHIN_USD"],2),1,0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"TOTAL CASH AVAILABLE FOR USE",1,0,'R',0);
        $this->Cell($w[1],5,FormatNumber($Current["CASH_AVAILABLE_IDR"]),1,0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Current["CASH_AVAILABLE_USD"],2),1,0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"CASH OUT FLOW","LR",0,'L',0);
        $this->Cell($w[1],5,"","LR",0,'L',0);
        $this->Cell($w[2],5,"","LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"","LR",0,'L',0);
        $this->Cell($w[1],5,"","LR",0,'L',0);
        $this->Cell($w[2],5,"","LR",0,'L',0);
        $this->Ln();
        $this->Cell($w[0],5,"OPERATIONAL CASH OUT FLOW","LR",0,'L',0);
        $this->Cell($w[1],5,"","LR",0,'R',0);
        $this->Cell($w[2],5,"","LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"   - PAID TO AFFILIATED","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Current["HUTANGAFILIASI_IDR"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Current["HUTANGAFILIASI_USD"],2),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"   - PAID TO THIRD PARTY","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Current["HUTANGNONAFILIASI_IDR"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Current["HUTANGNONAFILIASI_USD"],2),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"   - OTHERS","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Current["OVERHEAD_IDR"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Current["OVERHEAD_USD"],2),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"SUB TOTAL OPR CASH OUT","LR",0,'R',0);
        $this->Cell($w[1],5,"","LR",0,'R',0);
        $this->Cell($w[2],5,"","LR",0,'R',0);

        $this->Ln();
        $this->Cell($w[0],5,"NON OPERATIONAL CASH OUT FLOW","LR",0,'L',0);
        $this->Cell($w[1],5,"","LR",0,'R',0);
        $this->Cell($w[2],5,"","LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"   - LOAN PRINCIPAL","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Current["LOAN_PRINCIPAL_IDR"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Current["LOAN_PRINCIPAL_USD"],2),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"   - INTEREST PAYMENT","LR",0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Current["INTEREST_PAYMENT_IDR"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Current["INTEREST_PAYMENT_USD"],2),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"   SUB TOTAL NON OPR CASH OUT","LR",0,'R',0);
        $this->Cell($w[1],5,FormatNumber($Current["CASH_OUT_IDR"]),"LR",0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Current["CASH_OUT_USD"],2),"LR",0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"TOTAL CASH OUT",1,0,'R',0);
        $this->Cell($w[1],5,FormatNumber($Current["TOTAL_CASH_OUT_IDR"]),1,0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Current["TOTAL_CASH_OUT_USD"],2),1,0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"",1,0,'R',0);
        $this->Cell($w[1],5,"",1,0,'R',0);
        $this->Cell($w[2],5,"",1,0,'R',0);
        $this->Ln();
        $this->Cell($w[0],5,"ENDING BALANCE",1,0,'L',0);
        $this->Cell($w[1],5,FormatNumber($Current["ENDING_BALANCE_IDR"]),1,0,'R',0);
        $this->Cell($w[2],5,FormatNumber($Current["ENDING_BALANCE_USD"],2),1,0,'R',0);
        $this->Ln();
    }

    function Footer() {
        //Closure line
    }

}


?>
